﻿using System.Web.Mvc;
using MSAcademic.v2.Web.Areas.Admin.Models.Section;
using MSAcademic.v2.Infrastructure.SubSection;

namespace MSAcademic.v2.Web.Areas.Admin.Controllers
{
    public partial class SectionController : Controller
    {
        public ActionResult ContentSections(ContentSectionsModel model)
        {
            if (!model.CanAccess) { return RedirectToRoute("Index"); }

            return View(model);
        }

        public ActionResult SubSectionListForContentSection(SubSectionListForContentSectionModel model)
        {
            if (!model.CanAccess) { return RedirectToRoute("Index"); }

            return View(model);
        }

        public ActionResult NewSubSection(NewSubSectionModel model)
        {
            if (!model.CanAccess) { return RedirectToRoute("Index"); }

            return PartialView(model);
        }

        [HttpPost]
        public ActionResult NewSubSection(NewSubSectionModel model, bool? post)
        {
            if (!model.CanAccess) { return RedirectToRoute("Index"); }

            _sectionCommands.Create(model.name, model.contentId, model.thumbnailUrl);

            return RedirectToRoute("SubSectionListForContentSection", new { contentId = model.contentId });
        }


        public ActionResult EditSubSection(EditSubSectionModel model)
        {
            if (!model.CanAccess) { return RedirectToRoute("Index"); }

            return PartialView(model);
        }

        [HttpPost]
        public ActionResult EditSubSection(EditSubSectionModel model, bool? post)
        {
            if (!model.CanAccess) { return RedirectToRoute("Index"); }

            _sectionCommands.Edit(model.subSection.Id, model.subSection.Name, model.subSection.ThumbnailUrl);

            return RedirectToRoute("SubSectionListForContentSection", new { contentId = model.subSection.ContentSectionId });
        }


        public ActionResult DeleteSubSection(int subId)
        {
            //TODO: UA
            int contentSectionId = _sectionCommands.Delete(subId);

            return RedirectToRoute("SubSectionListForContentSection", new { contentId = contentSectionId });
        }
        //[HttpPost]
        //public ActionResult NewSubSection(int contentSectionId, string name)
        //{
        //    _sectionCommands.Create(contentSectionId, name);

        //    return RedirectToRoute("ContentSection", new { id = contentSectionId });
        //}

    }

    public partial class SectionController
    {
        private ISubSectionCommand _sectionCommands;
        private ISubSectionQueries _sectionQueries;

        public SectionController(
            ISubSectionCommand sectionCommands)
        {
            _sectionCommands = sectionCommands;
        }
    }
}